home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Minimalist Clock 1.0.2 / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-04  |  5.9 KB  |  282 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Minimalist Clock
  4.     version 1.0.2
  5.     
  6.     Written by: Paul Celestin
  7.     
  8.     This simple application does nothing but display a simple clock in
  9.     a rectangle on your display. You can move the clock around, and you
  10.     can also check the date by clicking once on the clock.
  11.     
  12.     941207 - 1.0.0 initial release
  13.     951215 - 1.0.2 updated for CW7
  14.  
  15. ---------------------------------------------------------------------- */
  16.  
  17.  
  18. /* ----------------------------------------------------------------------
  19. includes
  20. ---------------------------------------------------------------------- */
  21.  
  22. #include    "the_defines.h"
  23. #include    "the_globals.h"
  24. #include    "the_prototypes.h"
  25.  
  26. Boolean            gDone,
  27.                 gWNEImplemented,
  28.                 gInBackground,
  29.                 gDrawDate = FALSE;
  30. long            gTicksOld = 0, gTicksNew = 0;
  31. EventRecord        gTheEvent;
  32. MenuHandle        gAppleMenu,
  33.                 gFileMenu,
  34.                 gEditMenu;
  35. WindowPtr        gClock;
  36.  
  37. /* ----------------------------------------------------------------------
  38. main - here is where it all began...
  39. ---------------------------------------------------------------------- */
  40. void main()
  41. {
  42.     Initialize();
  43.     MainLoop();
  44.     ExitToShell();
  45. }
  46.  
  47. /* ----------------------------------------------------------------------
  48. Initialize
  49. ---------------------------------------------------------------------- */
  50. void Initialize()
  51. {
  52.     Handle    myMenuBar;
  53.     
  54.     InitGraf(&qd.thePort);
  55.     InitFonts();
  56.     FlushEvents(everyEvent, 0);
  57.     InitWindows();
  58.     InitMenus();
  59.     TEInit();
  60.     InitDialogs(0L);
  61.     InitCursor();
  62.  
  63.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  64.     if (myMenuBar == NIL)
  65.         SysBeep(1);
  66.     else
  67.         SetMenuBar(myMenuBar);
  68.     
  69.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  70.     if (gAppleMenu == NIL)
  71.         SysBeep(1);
  72.     else
  73.         AddResMenu(gAppleMenu,'DRVR');
  74.     
  75.     gFileMenu = GetMHandle(MENU_FILE_ID);
  76.     if (gFileMenu == NIL)
  77.         SysBeep(1);
  78.  
  79.     gEditMenu = GetMHandle(MENU_EDIT_ID);
  80.     if (gEditMenu == NIL)
  81.         SysBeep(1);
  82.     
  83.     DrawMenuBar();
  84. }
  85.  
  86. /* ----------------------------------------------------------------------
  87. MainLoop
  88. ---------------------------------------------------------------------- */
  89. void MainLoop()
  90. {
  91.     RgnHandle    cursorRgn;
  92.     Boolean        gotEvent;
  93.     
  94.     gDone = false;
  95.     gInBackground = false;
  96.     
  97.     cursorRgn = NewRgn();
  98.     
  99.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  100.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  101.     
  102.     CreateClock();
  103.     
  104.     while (gDone == false)
  105.     {
  106.         if (gWNEImplemented)
  107.         gotEvent =
  108.             WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  109.         else
  110.         {
  111.             SystemTask();
  112.             gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  113.         }
  114.         
  115.         if (gotEvent)
  116.             Do();
  117.         else
  118.             DoIdle();
  119.     }
  120. }
  121.  
  122. /* ----------------------------------------------------------------------
  123. Do
  124. ---------------------------------------------------------------------- */
  125. void Do()
  126. {
  127.     char    c;
  128.     
  129.     switch (gTheEvent.what)
  130.     {
  131.         case nullEvent:
  132.             DoIdle();
  133.             break;
  134.         case mouseDown:
  135.             DoMouseDown();
  136.             break;
  137.         case keyDown:
  138.         case autoKey:
  139.             c = gTheEvent.message & charCodeMask;
  140.             if ((gTheEvent.modifiers & cmdKey) != 0)
  141.             {
  142.                 DoMenu(MenuKey(c));
  143.             }
  144.             break;
  145.         case activateEvt:
  146.             break;
  147.         case updateEvt:
  148.             break;
  149.         case app4Evt:
  150.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  151.             {
  152.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  153.             }
  154.             else
  155.                 DoIdle();
  156.             break;
  157.     }
  158. }
  159.  
  160. /* ----------------------------------------------------------------------
  161. DoIdle
  162. ---------------------------------------------------------------------- */
  163. void DoIdle()
  164. {
  165.     gTicksNew = TickCount();
  166.     if ((gTicksNew - gTicksOld) > UPDATE_INTERVAL)
  167.     {
  168.         gTicksOld = gTicksNew;
  169.         UpdateClock();
  170.     }
  171. }
  172.  
  173. /* ----------------------------------------------------------------------
  174. DoUpdate
  175. ---------------------------------------------------------------------- */
  176. void DoUpdate(window)
  177. WindowPtr window;
  178. {
  179.     GrafPtr    savedPort;
  180.     
  181.     GetPort(&savedPort);
  182.     SetPort(window);
  183.     BeginUpdate(window);
  184.     UpdateClock();
  185.     EndUpdate(window);
  186.     SetPort(savedPort);
  187. }
  188.  
  189. /* ----------------------------------------------------------------------
  190. DoMouseDown
  191. ---------------------------------------------------------------------- */
  192. void DoMouseDown()
  193. {
  194.     WindowPtr    window;
  195.     short int    thePart;
  196.     long int    menuChoice, windSize, newSize;
  197.     Boolean        doZoom, doGoAway;
  198.     
  199.     thePart = FindWindow(gTheEvent.where,&window);
  200.     switch (thePart)
  201.     {
  202.         case inMenuBar:
  203.             menuChoice = MenuSelect(gTheEvent.where);
  204.             DoMenu(menuChoice);
  205.             break;
  206.         case inSysWindow:
  207.             SystemClick(&gTheEvent,window);
  208.             break;
  209.         case inContent:
  210.             if (window != FrontWindow())
  211.                 SelectWindow(window);
  212.             break;
  213.         case inGrow:
  214.             newSize = GrowWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  215.             SizeWindow(window,LoWord(newSize),HiWord(newSize),false);
  216.             DoUpdate(window);
  217.             break;
  218.         case inDrag:
  219.             gDrawDate = TRUE;
  220.             DragWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  221.             DoUpdate(window);
  222.             break;
  223.         case inZoomIn:
  224.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomIn))
  225.             {
  226.                 SizeWindow(window,200,200,false);
  227.                 DoUpdate(window);
  228.             }
  229.             break;
  230.         case inZoomOut:
  231.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomOut))
  232.             {
  233.                 short winHeight,winWidth;
  234.  
  235.                 winHeight = qd.screenBits.bounds.bottom - 43;
  236.                 winWidth = qd.screenBits.bounds.right - 5;
  237.                 SizeWindow(window,winWidth,winHeight,false);
  238.                 MoveWindow(window,2,LMGetMBarHeight() + 20,false);
  239.                 DoUpdate(window);
  240.             }
  241.             break;
  242.             break;
  243.         case inGoAway:
  244.             if (doGoAway = TrackGoAway(window,gTheEvent.where))
  245.                 CloseWindow(window);
  246.             break;
  247.         default:
  248.             break;
  249.     }
  250. }
  251.  
  252. /* ----------------------------------------------------------------------
  253. DoMenu
  254. ---------------------------------------------------------------------- */
  255. void DoMenu(menuChoice)
  256. long int menuChoice;
  257. {
  258.     int    theMenu;
  259.     int    theItem;
  260.     
  261.     if (menuChoice != 0)
  262.     {
  263.         theMenu = HiWord(menuChoice);
  264.         theItem = LoWord(menuChoice);
  265.         switch (theMenu)
  266.         {
  267.             case MENU_APPLE_ID:
  268.                 DoMenuApple(theItem);
  269.                 break;
  270.             case MENU_FILE_ID:
  271.                 DoMenuFile(theItem);
  272.                 break;
  273.             case MENU_EDIT_ID:
  274.                 DoMenuEdit(theItem);
  275.                 break;
  276.             default:
  277.                 break;
  278.         }
  279.         HiliteMenu(0);
  280.     }
  281. }
  282.